


% Input data
VarDecomp = csvread('source/VarDecompWUW_090119.csv');
MeanU_data = VarDecomp(1,1) ;
if strcmp(options.Comparison,'Weighted') == 1
    col = 1 ;   
elseif strcmp(options.Comparison,'Unweighted') == 1
    col = 2 ;
else
    error('Invalid options.Comparison')
end
StdU_data     = VarDecomp(2,col) ;
VarUpred_data = VarDecomp(4,col) ;
VarSep_data   = VarDecomp(5,col) ;
VarFind_data  = VarDecomp(6,col) ;
Cov_data      = VarDecomp(7,col) ;


% Compute welfare gains

WelfareGains_Z  = 100 * ( Welfare_Z / Welfare_CE - 1 ) ;
WelfareGains_SP = 100 * ( Welfare_SP / Welfare_CE - 1 ) ;

UnempGains_Z  = 100 * ( WMZ / WMC - 1 ) ;
UnempGains_SP = 100 * ( WMP / WMC - 1 ) ;

EmpGains_Z  = 100 * ( ( WMZ * WEZ ) / ( WMC * WEC ) - WMZ / WMC ) ;
EmpGains_SP = 100 * ( ( WMP * WEP ) / ( WMC * WEC ) - WMP / WMC ) ;

HumanCapGains_Z  = 100 * ( ( WMZ * WHZ * WEZ ) / ( WMC * WHC * WEC ) - ( WMZ * WEZ ) / ( WMC * WEC ) ) ;
HumanCapGains_SP = 100 * ( ( WMP * WHP * WEP ) / ( WMC * WHC * WEC ) - ( WMP * WEP ) / ( WMC * WEC ) ) ;


% disp(['  Unemployed          ' '-    ' '         ' num2str( 100 * ( WMP / WMC - 1 ) ,'%4.3f') ])
% disp(['  Employed            ' '-    ' '         ' num2str( 100 * ( WEP / WEC - 1 ) ,'%4.3f') ])
% disp(['  Human capital       ' '-    ' '         ' num2str( 100 * ( WHP / WHC - 1 ) ,'%4.3f') ])

disp(['Gains EZ = ' num2str(WelfareGains_Z,3)])
disp(['Gains SP = ' num2str(WelfareGains_SP,3)])

if strcmp(GAINSSAVE,'save') == 1
                       
    fid = fopen(['tables/' GAINSTITLE '.tex'],'w');

    fprintf(fid,'\\begin{tabular}{lccc}\n');
    
    fprintf(fid,' & Laissez-faire & EZ program & Quasi-optimal \\\\[1mm] \n');
    fprintf(fid,'\\hline \\\\[-1mm] \n');
    
    fprintf(fid,'Aggregate unemployment rate & %2.3f & %2.3f & %2.3f \\\\[1mm] \n' , [ MeanU_CE , MeanU_Z , MeanU_SP   ] ) ; %VarDecomp(2,1)
    fprintf(fid,'St. dev. unemployment rate & %2.3f& %2.3f & %2.3f \\\\[2mm] \n' , [ StdU_CE  , StdU_Z , StdU_SP  ] ) ;
    
    fprintf(fid,'\\hline \\\\[-1mm] \n');
    
    fprintf(fid,'Aggregate welfare gains (\\%%) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ WelfareGains_Z , WelfareGains_SP ] ) ; %VarDecomp(2,1)
    fprintf(fid,'\\hskip5mm Unemployed & - & %2.2f & %2.2f \\\\[1mm] \n'    , [ UnempGains_Z , UnempGains_SP ] ) ;
    fprintf(fid,'\\hskip5mm Employed & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ EmpGains_Z , EmpGains_SP ] ) ;
    fprintf(fid,'\\hskip5mm Human capital & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ HumanCapGains_Z , HumanCapGains_SP ] ) ;
    
    fprintf(fid,'\\hline\n');
    fprintf(fid,'\\end{tabular}');

    fclose(fid);
    
    
    
    
    % With P10 P90
    
    % Values at particular percentiles
    p = [ 1 , 99 ] ;
    [~,xLow] = min( ( cumsum(popC) - p(1)/100 ).^2 ) ;
    [~,xHigh] = min( ( cumsum(popC) - p(2)/100 ).^2 ) ;

    
    fid = fopen(['tables/' GAINSTITLE 'Full.tex'],'w');

    fprintf(fid,'\\begin{tabular}{lccc}\n');
    fprintf(fid,' & Laissez-faire & EZ program & Quasi-optimal \\\\[1mm] \n');
    fprintf(fid,'\\hline \\\\[-1mm] \n');
    
    fprintf(fid,'Aggregate unemployment rate & %2.3f & %2.3f & %2.3f \\\\[1mm] \n' , [ MeanU_CE , MeanU_Z , MeanU_SP   ] ) ; %VarDecomp(2,1)
    fprintf(fid,'St. dev. unemployment rate & %2.3f& %2.3f & %2.3f \\\\[2mm] \n' , [ StdU_CE  , StdU_Z , StdU_SP  ] ) ;
    
    fprintf(fid,'\\hline \\\\[-1mm] \n');

    fprintf(fid,'Unemployment rate change at P%1.0f (pp) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ p(1) , uZ(xLow)-uC(xLow) , uP(xLow)-uC(xLow) ] ) ; %VarDecomp(2,1)
    fprintf(fid,'Welfare gains at P%1.0f (\\%%) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ p(1) , Wg_Z(xLow) , Wg_SP(xLow) ] ) ; %VarDecomp(2,1)
    fprintf(fid,'\\hskip5mm Unemployed & - & %2.2f & %2.2f \\\\[1mm] \n'    , [ Ug_Z(xLow) , Ug_SP(xLow) ] ) ;
    fprintf(fid,'\\hskip5mm Employed & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ Eg_Z(xLow) , Eg_SP(xLow) ] ) ;
    fprintf(fid,'\\hskip5mm Human capital & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ Hg_Z(xLow) , Hg_SP(xLow) ] ) ;
    
    fprintf(fid,'\\hline \\\\[-1mm] \n');

    fprintf(fid,'Unemployment rate change at P%2.0f (pp) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ p(2) , uZ(xHigh)-uC(xHigh) , uP(xHigh)-uC(xHigh) ] ) ; %VarDecomp(2,1)
    fprintf(fid,'Welfare gains at P%2.0f (\\%%) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ p(2) , Wg_Z(xHigh) , Wg_SP(xHigh) ] ) ; %VarDecomp(2,1)
    fprintf(fid,'\\hskip5mm Unemployed & - & %2.2f & %2.2f \\\\[1mm] \n'    , [ Ug_Z(xHigh) , Ug_SP(xHigh) ] ) ;
    fprintf(fid,'\\hskip5mm Employed & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ Eg_Z(xHigh) , Eg_SP(xHigh) ] ) ;
    fprintf(fid,'\\hskip5mm Human capital & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ Hg_Z(xHigh) , Hg_SP(xHigh) ] ) ;
    
    
    fprintf(fid,'\\hline \\\\[-1mm] \n');

    fprintf(fid,'Aggregate welfare gains (\\%%) & - & %2.2f & %2.2f \\\\[1mm] \n' , [ WelfareGains_Z , WelfareGains_SP ] ) ; %VarDecomp(2,1)
    fprintf(fid,'\\hskip5mm Unemployed & - & %2.2f & %2.2f \\\\[1mm] \n'    , [ UnempGains_Z , UnempGains_SP ] ) ;
    fprintf(fid,'\\hskip5mm Employed & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ EmpGains_Z , EmpGains_SP ] ) ;
    fprintf(fid,'\\hskip5mm Human capital & - & %2.2f & %2.2f \\\\[2mm] \n'   , [ HumanCapGains_Z , HumanCapGains_SP ] ) ;
    
    
    fprintf(fid,'\\hline\n');
    fprintf(fid,'\\end{tabular}');

    fclose(fid);
    
    
    
end


%     
% disp('**********************************************************')
% fprintf('\n')
% disp('                      COMP. EQ.     PLANNER')
% fprintf('\n')
% disp('Weighted')
% fprintf('\n')
% disp(['   Mean U-rate        ' num2str(MeanU_CE,'%4.3f') '         ' num2str(MeanU_SP,'%4.3f') ])
% disp(['   Std. U-rate        ' num2str(StdU_CE,'%4.3f') '         ' num2str(StdU_SP,'%4.3f')])
% fprintf('\n')
% disp(['   Var log(s/f)       ' num2str(vU_CE,'%4.3f') '         ' num2str(vU_SP,'%4.3f') ])
% fprintf('\n')
% disp(['      log(sep)        ' num2str(100*(vSep_CE +0.5*covSepFind_CE)/vU_CE,'%4.0f') '%' '          ' num2str(100*(vSep_SP +0.5*covSepFind_SP)/vU_SP,'%4.0f') '%'])
% disp(['      log(find)       ' num2str(100*(vFind_CE+0.5*covSepFind_CE)/vU_CE,'%4.0f') '%' '          ' num2str(100*(vFind_SP+0.5*covSepFind_SP)/vU_SP,'%4.0f') '%'])
% fprintf('\n')
% disp(['      Var log(sep)    ' num2str(100*vSep_CE/vU_CE,'%4.0f') '%'        '          ' num2str(100*vSep_SP/vU_SP,'%4.0f') '%'])
% disp(['      Var log(find)   ' num2str(100*vFind_CE/vU_CE,'%4.0f') '%'       '          ' num2str(100*vFind_SP/vU_SP,'%4.0f') '%'])
% disp(['      Covariance      ' num2str(100*covSepFind_CE/vU_CE,'%4.0f') '%'  '         ' num2str(100*covSepFind_SP/vU_SP,'%4.0f') '%'])
% %disp(['      Total                 = ' num2str(100*(vSep+vFind+covSepFind)/vU,4) ' %'])
% 
% fprintf('\n')
% 
% disp('Unweighted')
% fprintf('\n')
% disp(['   Mean U-rate        ' num2str(MeanU0_CE,'%4.3f') '         ' num2str(MeanU0_SP,'%4.3f') ])
% disp(['   Std. U-rate        ' num2str(StdU0_CE,'%4.3f') '         ' num2str(StdU0_SP,'%4.3f')])
% fprintf('\n')
% disp(['   Var log(s/f)       ' num2str(vU0_CE,'%4.3f') '         ' num2str(vU0_SP,'%4.3f') ])
% fprintf('\n')
% disp(['      log(sep)        ' num2str(100*(vSep0_CE +0.5*covSepFind0_CE)/vU0_CE,'%4.0f') '%' '          ' num2str(100*(vSep0_SP +0.5*covSepFind0_SP)/vU0_SP,'%4.0f') '%'])
% disp(['      log(find)       ' num2str(100*(vFind0_CE+0.5*covSepFind0_CE)/vU0_CE,'%4.0f') '%' '          ' num2str(100*(vFind0_SP+0.5*covSepFind0_SP)/vU0_SP,'%4.0f') '%'])
% fprintf('\n')
% disp(['      Var log(sep)    ' num2str(100*vSep0_CE/vU0_CE,'%4.0f') '%'        '          ' num2str(100*vSep0_SP/vU0_SP,'%4.0f') '%'])
% disp(['      Var log(find)   ' num2str(100*vFind0_CE/vU0_CE,'%4.0f') '%'       '          ' num2str(100*vFind0_SP/vU0_SP,'%4.0f') '%'])
% disp(['      Covariance      ' num2str(100*covSepFind0_CE/vU0_CE,'%4.0f') '%'  '         ' num2str(100*covSepFind0_SP/vU0_SP,'%4.0f') '%'])
% fprintf('\n')
% disp(['Output (%)            ' '-    ' '         ' num2str( 100 * ( Output_SP / Output_CE - 1 ) ,'%4.3f') ])
% disp(['Welfare (%)           ' '-    ' '         ' num2str( 100 * ( Welfare_SP / Welfare_CE - 1 ) ,'%4.3f') ])
% disp(['  Unemployed          ' '-    ' '         ' num2str( 100 * ( WMP / WMC - 1 ) ,'%4.3f') ])
% disp(['  Employed            ' '-    ' '         ' num2str( 100 * ( WEP / WEC - 1 ) ,'%4.3f') ])
% disp(['  Human capital       ' '-    ' '         ' num2str( 100 * ( WHP / WHC - 1 ) ,'%4.3f') ])
% fprintf('\n')
% disp('**********************************************************')
% 


